package xyz.scootaloo.oj.offer

import org.junit.Test

/**
 * @author flutterdash@qq.com
 * @since 2021/4/9 11:33
 */
class P13 {

    private fun movingCount(m: Int, n: Int, k: Int): Int {
        var count = 0
        val number = intArrayOf(0, 0, 0, 0, 0, 0)
        for (r in 0 until m) {
            for (c in 0 until n) {
//                if (r + c > k + 1)
//                    break
                if (check(number, k))
                    count++
                increase(number, 5)
            }
            increase(number, 2)
            for (idx in 3..5) // reset
                number[idx] = 0
        }
        return count
    }

    private fun check(number: IntArray, limit: Int): Boolean = number.sum() <= limit

    private fun increase(number: IntArray, pos: Int) {
        for (idx in pos downTo (pos - 2)) {
            if (number[idx] < 9) {
                number[idx]++
                break
            } else {
                number[idx] = 0
            }
        }
    }

    @Test
    fun test0() {
        println(movingCount(2, 3, 1))
        println(movingCount(3, 1, 0))
        println(movingCount(16, 8, 4))
        println(movingCount(11, 8, 16))
    }

}
